<?php

namespace backend\services\system;

use backend\extensions\ApiBaseService;
use backend\models\system\Admin;
use backend\services\captcha\CaptchaService;
use backend\services\system\MenuService;
use backend\models\system\Role;
use backend\models\system\Menu;

/**
 * Desc 管理员服务类
 * @author HUI
 */
class AdminService extends ApiBaseService {

    /**
     * 登录
     * @param array $params 参数 
     * * */
    public static function login($params) {

        if (CaptchaService::validateCode($params['code']) == false) {
            //return self::jsonError('验证码异常,请检查');
        }
        $admin = Admin::getDataOne(['account' => $params['account']]);
        if (empty($admin)) {
            return self::jsonError('账号异常，请检查');
        }
        if ($admin['password'] != md5(trim($params['password']) . $admin['create_time'])) {
            return self::jsonError('密码异常，请检查');
        }
        $token = md5($params['account'] . $admin['password']);
        $data = [
            'token' => $token,
            'admin' => [
                'account'=>$admin['account'],
                'realname'=>$admin['realname'],
                'sex'=>$admin['sex'],
            ],
        ];
        \common\services\cache\CommonCache::setCache($token, $admin);
        return self::jsonSuccess($data);
    }
    
    /**
     * 获取授权菜单
     * * */
    public static function getMenu() {
        $admin = Admin::getDataByToken();
        if (empty($admin)) {
            return self::jsonError('账号权限不足，请检查');
        }
        $params = [
            'status'=> Menu::STATUS_ENABLE,
        ];
        if($admin['role_id'] != Role::SUPER_ADMIN){
            $role = Role::getRoleByAdmin(trim($admin['role_id'],','));
            $params['id'] = array_column(Menu::getMenuByRole(array_column($role, 'menu_id')), 'id');
        }
        $data = MenuService::getList($params,['id','parent_id','name','type','icon','route','path','page']);
        if(empty($data)){
            return self::jsonError('账号权限不足，请检查');
        }
        return self::jsonSuccess($data);
    }

    /**
     * 列表
     * @param array $params 参数 
     * * */
    public static function getList($params) {
        //检索条件
        $condition = ['AND'];
        $condition[] = ['!=','role_id',Role::SUPER_ADMIN];
        if ($params['name']) {
            $condition[] = ['like', 'name', $params['name']];
        }
        if ($params['phone']) {
            $condition[] = ['=', 'phone', $params['phone']];
        }
        $list = Admin::getDataAll($condition, $params['page'], $params['page_size']);
        foreach ($list['list'] as &$value) {
            unset($value['password']);
            $value['status_name'] = Admin::$status[$value['status']];
            $value['sex_name'] = Admin::$sex[$value['sex']];
            $role = Role::getRoleByAdmin($value['role_id']);
            $value['role_name'] = empty($role)?'-': implode(',', array_column($role, 'name'));
        }
        return $list;
    }

    /**
     * 新增
     * @param array $params 参数 
     * * */
    public static function create($params) {
        if (Admin::getDataOne(['account' => $params['account']])) {
            return self::jsonError('账号已存在');
        }
        if(empty($params['password'])){
            return self::jsonError('请输入密码');
        }
        if (Admin::create($params) == false) {
            return self::jsonError('保存失败');
        }
        return self::jsonSuccess();
    }

    /**
     * 新增
     * @param array $params 参数 
     * * */
    public static function update($params) {
        $condition = ['id' => $params['id']];
        unset($params['id']);
        if (Admin::updateData($params, $condition) === false) {
            return self::jsonError('更新失败');
        }
        return self::jsonSuccess();
    }

    /**
     * 删除
     * @param array $params 参数 
     * * */
    public static function delete($params) {
        $condition = ['id' => $params['id']];
        if (Admin::del($condition) == false) {
            return self::jsonError('删除失败');
        }
        return self::jsonSuccess();
    }
    
     /**
     * 导出
     * @param array $params 参数 
     * * */
    public static function Export($data){
        $title = [
            'account'=>'账号',
            'realname'=>'真实姓名',
            'phone'=>'手机号',
            'role_name'=>'权限',
            'sex_name'=>'性别',
        ];
        return \Yii::$app->excel->exportCsv($title, $data);
    }
    
    /**
     * 导入
     * @param array $params 参数 
     * * */
    public static function Import($files){
        $data = \Yii::$app->excel->Import($files);
        return self::jsonSuccess();
    }

}
